package com.estar.integaration.Controller;


import com.estar.integaration.Service.ICartService;
import com.estar.integaration.Service.IUserService;
import com.estar.integaration.Util.Util;
import com.estar.integaration.entity.Cart;
import com.estar.integaration.entity.NoSQL.Res;
import com.estar.integaration.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YJL
 * @since 2021-11-18
 */
@RestController
@RequestMapping("/user")
public class UserController {

  @Autowired
  IUserService userService;
  @Autowired
  ICartService cartService;


  @GetMapping("/login")
  public Res login(@RequestParam String userName, @RequestParam String password){
    //TODO  登陆后返回购物车
    User user =  userService.findByUserName(userName);
    if(user == null)
      return new Res("40000","fa");//登录失败
    if(user.getPassword().equals(password)){
        Cart cart = cartService.findCartByUserId(user.getId());
        if(cart == null){//如果是空就生成一个扔里面
          cart = new Cart(Util.getUUID(),user.getId());
          cartService.insert(cart);
        }
    }else {
      return new Res("40000","fa");//登录失败
    }
    return new Res("20000","suc");//登录成功
  }

}
